const myRace = (promises) => {
  // 参数校验
  if (!Array.isArray(promises) || promises.length === 0) {
    return Promise.reject(new TypeError("Argument must be an array"));
  }

  // 有一个fullfilled或者rejected就返回
  return new Promise((resolve, reject) => {
    for (const pms of promises) {
      pms
        .then((value) => {
          resolve(value);
        })
        .catch((err) => {
          reject(err);
        });
    }
  });
};

// 验证
const p1 = new Promise((resolve) => setTimeout(() => resolve("p1"), 1000));
const p2 = new Promise((resolve) => setTimeout(() => resolve("p2"), 500));
const p3 = new Promise((resolve) => setTimeout(() => resolve("p3"), 200));

// 正确写法：会输出 'p3'（最快的一个）
myRace([p1, p2, p3]).then(console.log);
